package ar.com.PanteraRosa;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import ar.com.PanteraRosa.Business.UserController;
import ar.com.PanteraRosa.Common.Conf;
import ar.com.PanteraRosa.Entity.User;

public class EditUserActivity extends Activity {

    EditText userNick;
    EditText userPassword;
    EditText userFirstName;
    EditText userLastName;
    EditText userEmail;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.edituser);
	    setTitle(R.string.app_name);

	    userNick = (EditText) findViewById(R.id.txtUser);
	    userPassword = (EditText) findViewById(R.id.txtPass);
	    userFirstName = (EditText) findViewById(R.id.txtFirstName);
	    userLastName = (EditText) findViewById(R.id.txtLastName);
	    userEmail = (EditText) findViewById(R.id.txtEmail);
    }

//    public void btnVolver_OnClick(View vista) throws JSONException {
//	if (Conf.getInstance().isLogged()) {
//	    Intent intent = new Intent();
//	    intent.setClass(this, LoginUserActivity.class);
//	    startActivity(intent);
//	}
//    }

    public void btnAceptar_OnClick(View vista) throws JSONException {
	Conf.getInstance().setLogged(false);
	
	if (!Conf.getInstance().isLogged()) {
		if (!userNick.getText().toString().equals("") && !userPassword.getText().toString().equals("")
				&& !userFirstName.getText().toString().equals("") && !userLastName.getText().toString().equals("")
					&& !userEmail.getText().toString().equals(""))
		{
		    User user = new User(0, userNick.getText().toString(), userPassword
			    .getText().toString(), userFirstName.getText().toString(),
			    userLastName.getText().toString(), userEmail.getText()
				    .toString());
	
		    UserController userController = new UserController(this);
	
		    userController.insertUserOnCloud(user);
		    userController.updateUsersOnMobile();
	
		    Intent intent = new Intent();
		    intent.setClass(this, HomeUserActivity.class);
		    startActivity(intent);
		}
		else
		{
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);

    		alert.setTitle("Error!");
    		alert.setMessage("Llamalo a Fede.");

    		alert.setPositiveButton("Ok",null);
    		alert.show();
		}
	}
    }
}
